package com.shen.jcbf.thread;


/**
 * @author 怜笑
 * @company 公司
 * @create 2019-08-07 10:35
 */
public class TestVolatile002 {

    public static void main(String[] args) {
        TestVt testVt = new TestVt();
        new Thread(testVt).start();

        while (true){
            if (testVt.isFlag()){
                System.out.println("--------");
                break;
            }
        }
    }

}

class TestVt implements Runnable {

    private volatile boolean flag = false;

    @Override
    public void run() {
        try {
            Thread.sleep(200);
            flag = true;
            System.out.println("flag = " + flag);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}